package vues;

import java.awt.Dimension;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import evenements.EcouteurControle;

public class PanelDroite extends JPanel{


	private static final long serialVersionUID = 1L;
	private JPanel panControle, panStats;
	private JLabel lblDebut = new JLabel("Début :");
	private JLabel lblTemps = new JLabel("Temps :");
	private JLabel lblFin = new JLabel("Fin :");
	private JLabel valeurDebut = new JLabel("");
	private JLabel valeurTemps = new JLabel("");
	private JLabel valeurFin = new JLabel("");
	private JLabel intervalle;
	private SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
	private SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");
	private JButton vitessePlus, vitesseMoins; 
	
	
	public PanelDroite(IHM ihm){
		this.panControle = new JPanel();
		this.panControle.setBorder(BorderFactory.createTitledBorder("Contrôle"));
		this.intervalle = new JLabel(ihm.getHorloge().getTimer().getDelay()+" ms");
		JLabel lblIntervalle = new JLabel("Un mouvement tous les : ");
		this.intervalle.setPreferredSize(new Dimension(100, 30));
		this.vitesseMoins = new JButton(new ImageIcon(getClass().getResource("moins-icon.png")));
		this.vitesseMoins.setActionCommand("moins");
		this.vitesseMoins.addActionListener(new EcouteurControle(ihm));
		this.vitessePlus = new JButton(new ImageIcon(getClass().getResource("plus-icon.png")));
		this.vitessePlus.setActionCommand("plus");
		this.vitessePlus.addActionListener(new EcouteurControle(ihm));
		this.panControle.add(lblIntervalle);
		this.panControle.add(intervalle);
		this.panControle.add(vitesseMoins);
		this.panControle.add(vitessePlus);
		this.panStats = new JPanel();
		this.lblDebut.setVisible(false);
		this.valeurDebut.setPreferredSize(new Dimension(220,20));
		this.lblTemps.setVisible(false);
		this.valeurTemps.setPreferredSize(new Dimension(220,20));
		this.lblFin.setVisible(false);
		this.valeurFin.setPreferredSize(new Dimension(220,20));
		this.panStats.setBorder(BorderFactory.createTitledBorder("Statistiques"));
		this.panControle.setPreferredSize(new Dimension(300,300));
		this.panStats.setPreferredSize(new Dimension(300,500));
		this.setPreferredSize(new Dimension(300,900));
		this.panStats.add(lblDebut);
		this.panStats.add(valeurDebut);
		this.panStats.add(lblTemps);
		this.panStats.add(valeurTemps);
		this.panStats.add(lblFin);
		this.panStats.add(valeurFin);
		this.add(this.panControle);
		this.add(this.panStats);
	}

	public String getValeurDebut() {
		return valeurDebut.getText();
	}

	public void setValeurDebut(String valeurDebut) {
		this.valeurDebut.setText(valeurDebut);
	}

	public JLabel getIntervalle() {
		return intervalle;
	}

	public void setIntervalle(JLabel intervalle) {
		this.intervalle = intervalle;
	}

	public JLabel getValeurTemps() {
		return valeurTemps;
	}

	public void setValeurTemps(Date valeurTemps) {
		this.valeurTemps.setText(sdf2.format(valeurTemps));
	}
	
	public void afficherStatEnCours(){
		this.lblDebut.setVisible(true);
		this.valeurDebut.setText(sdf.format(new Date()));
		this.lblTemps.setVisible(true);
	}
	
	public void afficherStatEnFin(){
		this.lblFin.setVisible(true);
		this.valeurFin.setText(sdf.format(new Date()));
	}

	public void setValeurFin(String valeurFin) {
		this.valeurFin.setText(valeurFin);
		
	}

	public void MiseAJourTempsSimulation(){
		long diff = 0;
		long diffSeconds = 0;
		long diffMinutes = 0;
		long diffHours = 0;
		try {
			diff = sdf.parse(sdf.format(new Date().getTime())).getTime() - sdf.parse(sdf.format(sdf.parse(this.valeurDebut.getText()).getTime())).getTime();
			 diffSeconds = diff / 1000 % 60;
			 diffMinutes = diff / (60 * 1000) % 60;
			 diffHours = diff / (60 * 60 * 1000) % 24;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			String datediff = "01/01/15 "+diffHours+":"+diffMinutes+":"+diffSeconds;
			
			this.valeurTemps.setText(sdf2.format(sdf.parse(datediff)));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
